const APIKEY_L = "b9496db4ca924f92b05972d698596777";

Page({
  data: {
    location: "", // 梧州市的经纬度
    locationText: "", // 初始化位置按钮文本
    airQualityIndex: "", // 空气质量指数
    airQualityLevel: "", // 空气质量级别
    pm25: "", // PM2.5值
    pm10: "", // PM10值
    o3: "", // O3值
    so2: "", // SO2值
    uvIndex: "", // 紫外线指数
    sportIndex: "", // 运动指数
    travelIndex: "", // 旅游指数
    dressingIndex: "", // 穿衣指数
    carWashIndex: "", // 洗车指数
    fishingIndex: "", // 钓鱼指数
    coldIndex:"",//感冒指数
    fieldIndex: "",//晾晒指数
    sunscreenIndex:""//防晒指数
  },

  onLoad(options) {
    this.getLocation();
  },

 /**
   * 获取定位
   */
  getLocation() {
    var that = this;
    wx.getLocation({
      type: 'gcj02',
      success(res) {
        that.setData({
          location: res.longitude + "," + res.latitude
        });
        that.getAirQuality();
        that.getIndices();
        // 停止下拉刷新
        wx.stopPullDownRefresh();
      },
      fail(err) {
        wx.showModal({
          title: '获取定位信息失败',
          content: '为了给您提供准确的天气预报服务,请在设置中授权【位置信息】',
          success(mRes) {
            if (mRes.confirm) {
              wx.openSetting({
                success: function (data) {
                  if (data.authSetting["scope.userLocation"] === true) {
                    wx.showToast({
                      title: '授权成功',
                      icon: 'success',
                      duration: 1000
                    })
                    that.getLocation();
                  } else {
                    wx.showToast({
                      title: '授权失败',
                      icon: 'none',
                      duration: 1000
                    });
                    that.setData({
                      location: "116.41,39.92"
                    });
                    that.getWeather();
                    that.getCityByLoaction();
                    // 停止下拉刷新
                    wx.stopPullDownRefresh();
                  }
                },
                fail(err) {
                  console.log(err);
                  wx.showToast({
                    title: '唤起设置页失败，请手动打开',
                    icon: 'none',
                    duration: 1000
                  });
                  that.setData({
                    location: "116.41,39.92"
                  });
                  that.getWeather();
                  that.getCityByLoaction();
                  // 停止下拉刷新
                  wx.stopPullDownRefresh();
                }
              });
            } else if (mRes.cancel) {
              that.setData({
                location: "116.41,39.92"
              });
              that.getWeather();
              that.getCityByLoaction();
              // 停止下拉刷新
              wx.stopPullDownRefresh();
            }
          }
        });
      }
    });
  },

  // 获取空气质量信息
  getAirQuality() {
    var that = this;
    wx.request({
      url: 'https://devapi.qweather.com/v7/air/now?key=' + APIKEY_L + "&location=" + that.data.location,
      success(result) {
        var res = result.data;
        if (res.code === "200") {
          var airQuality = res.now;
          that.setData({
            airQualityIndex: airQuality.aqi,
            airQualityLevel: airQuality.category,
            pm25: airQuality.pm2p5,
            pm10: airQuality.pm10,
            o3: airQuality.o3,
            so2: airQuality.so2,
            co: airQuality.co,
            no2: airQuality.no2
          });
        } else {
          wx.showToast({
            title: '获取空气质量信息失败',
            icon: 'none'
          });
        }
      },
      fail() {
        wx.showToast({
          title: '获取空气质量信息失败',
          icon: 'none'
        });
      }
    });
  },

  // 获取生活指数信息
  getIndices() {
    var that = this;
    wx.request({
      url: 'https://devapi.qweather.com/v7/indices/1d?key=' + APIKEY_L + "&type=1,2,3,4,5,6,9,14,16&location=" + that.data.location,
      success(result) {
        var res = result.data;
        if (res.code === "200") {
          that.setData({
            sportIndex: that.getIndexValue(res.daily, '1'),
            carWashIndex: that.getIndexValue(res.daily, '2'),
            dressingIndex: that.getIndexValue(res.daily, '3'),
            fishingIndex: that.getIndexValue(res.daily, '4'),
            uvIndex: that.getIndexValue(res.daily, '5'),
            travelIndex: that.getIndexValue(res.daily, '6'),
            coldIndex:that.getIndexValue(res.daily,'9'),
            fieldIndex:that.getIndexValue(res.daily,'14'),
            sunscreenIndex: that.getIndexValue(res.daily,'16')

          });
        } else {
          wx.showToast({
            title: '获取生活指数信息失败',
            icon: 'none'
          });
        }
      },
      fail() {
        wx.showToast({
          title: '获取生活指数信息失败',
          icon: 'none'
        });
      }
    });
  },

  // 辅助函数：根据类型获取指数值
  getIndexValue(indices, type) {
    var index = indices.find(index => index.type === type);
    return index ? index.category : '';
  },


  navigateToDetail() {
    // 携带空气质量信息跳转到详细页面
    wx.navigateTo({
      url: '/pages/lifeDetail/lifeDetail?airQualityIndex=' + this.data.airQualityIndex +
        '&airQualityLevel=' + this.data.airQualityLevel +
        '&pm25=' + this.data.pm25 +
        '&pm10=' + this.data.pm10 +
        '&o3=' + this.data.o3 +
        '&so2=' + this.data.so2 +
        '&co=' + this.data.co +
        '&no2=' + this.data.no2 +
        '&location=' + this.data.location
    });


  },
  onShareAppMessage() {

  },

  onPullDownRefresh() {
    this.getLocation();
  }
})